package com.enzism.springboot.quartz.controller;


import com.enzism.springboot.quartz.job.HelloJob;
import com.enzism.springboot.quartz.model.JobEntity;
import com.enzism.springboot.quartz.result.SifarResult;
import com.enzism.springboot.quartz.service.QuartzManagerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Api(value = "定时任务")
@RestController
public class QuartzController {
    @Autowired
    private Scheduler scheduler;

    @Autowired
    private QuartzManagerService quartzManagerService;

    @ApiOperation(value = "添加定时任务")
    @PostMapping("/add")
    public SifarResult addJob(@RequestBody JobEntity job) {

        SifarResult result = quartzManagerService.addJob(job, HelloJob.class);

        return result;
    }


    @ApiOperation(value = "获取所有任务列表")
    @GetMapping("/getList")
    public SifarResult getJobList() {

        return quartzManagerService.getJobList();
    }

    /**
     * 暂停任务
     * @param jobName
     * @param groupName
     * @return
     */
    @ApiOperation(value = "暂停任务")
    @PostMapping("/pauseJob")
    public SifarResult pauseJob(@RequestParam("jobName")String jobName,
                           @RequestParam("groupName")String groupName){

        quartzManagerService.pauseJob(jobName,groupName);
        return SifarResult.success();
    }

    /**
     * 启动任务
     * @param jobName
     * @param groupName
     * @return
     */
    @ApiOperation(value = "启动任务")
    @PostMapping("/resumeJob")
    public SifarResult resumeJob(@RequestParam("jobName")String jobName,
                            @RequestParam("groupName")String groupName){

        quartzManagerService.resumeJob(jobName,groupName);
        return SifarResult.success();
    }

    /**
     * 编辑
     * @param jobEntity
     * @return
     */
    @ApiOperation(value = "编辑任务")
    @PostMapping("/editJob")
    public SifarResult editJob(@RequestBody JobEntity jobEntity){
        quartzManagerService.editJob(jobEntity);
        return SifarResult.success();
    }

    /**
     * 删除任务
     * @param jobName
     * @return
     */
    @ApiOperation(value = "删除任务")
    @PostMapping("/deleteJob")
    public SifarResult deleteJob(@RequestParam("jobName")String jobName,
                            @RequestParam("groupName")String groupName){

        quartzManagerService.deleteJob(jobName,groupName);

        return SifarResult.success();
    }

}
